<template>
  <div class="app-container">
    <div class="mainbox">
      <div class="filter-container">
        <el-form v-show="showSearch" ref="queryForm" :model="queryParams"
                 :inline="true" label-width="68px" @submit.native.prevent>
          <el-form-item label="消息标题" prop="messageTitle">
            <el-input
              v-model.trim="queryParams.messageTitle"
              maxlength="30"
              placeholder="请输入内容"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="发送状态" prop="sendStatus">
            <el-select v-model="queryParams.sendStatus" placeholder="请选择" clearable
                       size="small">
              <el-option
                v-for="dict in dict.type.sendStatus"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div class="flex-1" />
        <div>
          <div class="flex-1" />
          <div style="display:flex;">
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
          </div>
        </div>
      </div>
      <div class="table-container table-fullscreen">
        <div class="table-opt-container">
          <div class="flex-1" />
          <dt-dialog-column v-model="isShowTable" :columns="showColumns" :table-ref="$refs.table"
                            @queryTable="getList" />
        </div>
        <el-table v-if="isShowTable" ref="table" v-loading="loading"
                  border highlight-current-row :header-cell-style="{ backgroundColor: '#f2f2f2'}"
                  :data="MessageList" @selection-change="handleSelectionChange">
          <template slot="empty">
            <p>{{ $store.getters.dataText }}</p>
          </template>
          <!-- <el-table-column fixed="left" type="selection" width="55" align="center" /> -->
          <el-table-column
            fixed="left"
            type="index"
            label="序号"
            width="70"
            :index="(index)=>(queryParams.pageNo - 1) * queryParams.pageSize + index + 1"
          />
          <el-table-column v-if="showColumns[0].show" key="messageTitle" label="消息标题"
                           show-overflow-tooltip align="center" prop="messageTitle" />
          <el-table-column v-if="showColumns[1].show" key="messageContent" label="消息内容"
                           show-overflow-tooltip align="center" prop="messageContent" />
          <el-table-column v-if="showColumns[2].show" key="channelSdkName" label="渠道"
                           show-overflow-tooltip align="center" prop="channelSdkName">
                           <!-- <template slot-scope="scope">
              <span>{{ getChannelName(scope.row) }}</span>
            </template> -->
          </el-table-column>
          <el-table-column v-if="showColumns[3].show" key="sender" label="发送方"
                           show-overflow-tooltip align="center" prop="sender" />
          <el-table-column v-if="showColumns[4].show" key="receiver" label="接收方"
                           show-overflow-tooltip align="center" prop="receiver" />
          <el-table-column v-if="showColumns[5].show" key="sendStatus" label="发送状态"
                           show-overflow-tooltip align="center" prop="sendStatus">
            <template slot-scope="scope">
              <dt-dict-tag :options="dict.type.sendStatus" :value="scope.row.sendStatus" />
            </template>
          </el-table-column>
          <el-table-column v-if="showColumns[6].show" key="sendTimes" label="发送次数总计"
                           show-overflow-tooltip align="center" prop="sendTimes"
                           width="180" />
          <el-table-column v-if="showColumns[7].show" key="failReason" label="失败描述"
                           show-overflow-tooltip align="center" prop="failReason" />
          <el-table-column v-if="showColumns[8].show" key="createTime" label="创建时间"
                           show-overflow-tooltip align="center" prop="createTime"
                           width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="showColumns[9].show" key="scheduledSendTime" label="发送时间"
                           show-overflow-tooltip align="center" prop="scheduledSendTime"
                           width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.scheduledSendTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="showColumns[10].show" key="updateTime" label="修改时间"
                           show-overflow-tooltip align="center" prop="updateTime"
                           width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
          </el-table-column>
          <el-table-column :key="Math.random()" label="操作" fixed="right"
                           align="center" class-name="small-padding fixed-width" width="162">
            <template slot-scope="scope">
              <el-button
                size="small"
                @click="handleDetail(scope.row)"
              >详情</el-button>
              <el-button
                size="small"
                :disabled="scope.row.sendTimes<6 ||scope.row.sendStatus == 2"
                @click="handleResetSend(scope.row)"
              >重发</el-button>
            </template>
          </el-table-column>
        </el-table>

        <dt-pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
    <!-- 详情弹框 -->
    <dt-detail ref="dtDetailRef" :dt-detail-option="detailOption" />
    <log-detail ref="logDetailRef" />
  </div>
</template>

<script>
import { sysMessageCenterPage, retrySendMessage } from '@/framework/api/messageCenter/messageLog'
import logDetail from './components/logDetail.vue'

export default {
  name: 'Message',
  dicts: ['way', 'sendStatus'],
  components: { logDetail },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显隐表格
      isShowTable: true,
      // 选中数组
      ids: [],
      // 列显隐数组
      showColumns: [
        { prop: 'messageTitle', label: '消息标题', show: true },
        { prop: 'messageContent', label: '消息内容', show: true },
        { prop: 'channelSdkName', label: '渠道', show: true },
        { prop: 'sender', label: '发送方', show: true },
        { prop: 'receiver', label: '接收方', show: true },
        { prop: 'sendStatus', label: '发送状态', show: true },
        { prop: 'sendTimes', label: '发送次数总计', show: true },
        { prop: 'failReason', label: '失败描述', show: true },
        { prop: 'createTime', label: '创建时间', show: true },
        { prop: 'scheduledSendTime', label: '发送时间', show: true },
        { prop: 'updateTime', label: '修改时间', show: true }

      ],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 系统消息表格数据
      MessageList: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        messageTitle: null
      },
      channelName: ''

    }
  },
  computed: {
    // 详情参数
    detailOption() {
      return {
        rows: [
          {
            label: '消息标题',
            prop: 'messageTitle'

          },
          {
            label: '消息内容',
            prop: 'messageContent'

          },
          {
            label: '渠道',
            prop: 'channelSdkName'

          },
          {
            label: '发送方',
            prop: 'sender'

          },
          {
            label: '接收方',
            prop: 'receiver'

          },
          {
            type: 'dict',
            options: this.dict.type.sendStatus,
            label: '发送状态',
            prop: 'sendStatus'

          },
          {
            label: '发送次数总计',
            prop: 'sendTimes'

          },
          {
            label: '失败描述',
            prop: 'failReason'
          },
          {
            label: '创建时间',
            prop: 'createTime'
          },
          {
            label: '发送时间',
            prop: 'scheduledSendTime'
          },
          {
            label: '修改时间',
            prop: 'updateTime'
          }
        ],
        data: {}
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询系统消息列表 */
    getList() {
      this.loading = true
      sysMessageCenterPage(this.queryParams).then(({ data: response }) => {
        this.MessageList = response.rows
        this.total = response.totalRows
        this.loading = false
      })
    },
    // 获取发送渠道
    getChannelName(row) {
      // if (row.channelSdkName.length > 0) {
      //   row.channelName = row.channelSdkName.map((resp) => {
      //     return resp.channelName
      //   }).join()
      //   return row.channelName
      // } else {
      //   row.channelName = '-'
      //   return row.channelName
      // }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    handleReset() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.messageId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleDetail(row) {
      this.$refs.logDetailRef.detail(row)
    },
    handleResetSend(row) {
      this.showScreenLoading()
      retrySendMessage({ messageId: row.messageId }).then((res) => {
        this.$dtModal.msgSuccess('重发成功')
        this.getList()
        this.hideScreenLoading()
      }).catch((res) => {
        this.hideScreenLoading()
      })
      // this.detailOption.data = row
      // this.$refs.dtDetailRef.dialogVisible = true
    }
  }
}
</script>
